/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import com.flan.stock.R;
import com.flan.stock.bean.News;
import com.flan.stock.utils.AsyncImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 新闻资讯要闻页面中的Listview空间的适配器
 * @author flan
 * @date   2015年11月7日
 */
public class NewsYwListViewAdapter extends BaseAdapter {
	
	private List<News> newsList;
	private Context context;
	//private SimpleAsyncImageLoader asyncImageLoader;
	private AsyncImageLoader asyncImageLoader;
	private DateFormat df = new SimpleDateFormat("HH:mm",Locale.CHINA);
	
	public NewsYwListViewAdapter(Context context,List<News> newsList) {
		super();
		this.context = context;
		this.newsList = newsList;
		//asyncImageLoader = new SimpleAsyncImageLoader();
		asyncImageLoader = new AsyncImageLoader(context, 5);
	}
	
	/**
	 * 设置ListView中的数据
	 * @param newsList
	 */
	public void setDate(List<News> newsList){
		this.newsList = newsList;
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		
		if(newsList != null){
			return newsList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(newsList != null){
			return newsList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void clean(){
		this.newsList.clear();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if(convertView == null){
			
			holder = new ViewHolder();
			
			convertView = LayoutInflater.from(context).inflate(R.layout.item_news_yw, parent, false);
			holder.img = (ImageView) convertView.findViewById(R.id.img_item);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		News news = newsList.get(position);
		String time = df.format(news.getCreateTime());
		
		holder.tv_title.setText(news.getTitle());
		holder.tv_time.setText(time);
		
		
		asyncImageLoader.DisplayImage(news.getImgUrl(), holder.img);
		/*String imgUrl = news.getImgUrl();
		holder.img.setTag(imgUrl);
		
		Drawable cacheDrawable = asyncImageLoader.loadDrawable(imgUrl, new ImageCallback() {
			@Override
			public void imageLoaded(String imgUrl, Drawable drawable) {
				ImageView imageView = (ImageView) listView.findViewWithTag(imgUrl);
				if(imageView != null){
					imageView.setImageDrawable(drawable);
				}
			}
		});
		
		if(cacheDrawable == null){
			holder.img.setBackgroundColor(Color.WHITE);
		}else {
			holder.img.setImageDrawable(cacheDrawable);
		}*/
		
		return convertView;
	}
	
	/**
	 * 用于保存ListView中重用的item视图的引用
	 * @author flan
	 * @date   2015年10月28日
	 */
	class ViewHolder {
		public ImageView img;
		public TextView tv_title,tv_time;
	}

}














